<?php
/*
Plugin Name: Visual Composer Youtube Iframe
Plugin URI: http://www.facebook.com/NguyenDuyPhong/
Description: Showing video based on Youtube Iframe.
Version: 1.0
Author: PhongND
Author URI: http://www.facebook.com/NguyenDuyPhong/
License: GPLv2 or later
*/


// don't load directly
if (!defined('ABSPATH')) die('-1');

class VC_Youtube_Iframe {
    function __construct() {
        // We safely integrate with VC with this hook
        add_action( 'init', array( $this, 'integrateWithVC' ) );
 
        // Use this when creating a shortcode addon
        add_shortcode( 'vc_youtube01', array( $this, 'render_vc_youtube01' ) );

        // Register CSS and JS
        add_action( 'wp_enqueue_scripts', array( $this, 'loadCssAndJs' ) );
    }
 
    public function integrateWithVC() {
        // Check if Visual Composer is installed
        if ( ! defined( 'WPB_VC_VERSION' ) ) {
            // Display notice that Visual Compser is required
            add_action('admin_notices', array( $this, 'showVcVersionNotice' ));
            return;
        }
 
        /*
        Add your Visual Composer logic here.
        Lets call vc_map function to "register" our custom shortcode within Visual Composer interface.

        More info: http://kb.wpbakery.com/index.php?title=Vc_map
        */
        vc_map( array(
            "name" => __("VC Youtube Iframe Shortcode", 'domain_vc_youtube01'),
            "description" => __("VC Youtube Iframe description text", 'domain_vc_youtube01'),
            "base" => "vc_youtube01",
            "class" => "",
            "controls" => "full",
            "icon" => plugins_url('assets/asterisk_yellow.png', __FILE__), // or css class name which you can reffer in your css file later. Example: "domain_vc_youtube01_my_class"
            "category" => __('Content', 'js_composer'),
            // 'admin_enqueue_js' => array(plugins_url('assets/hw_cssjs.js', __FILE__)), // js for backend editor
            //'admin_enqueue_css' => array(plugins_url('assets/hw_cssjs_admin.css', __FILE__)), // css for VC backend editor
            "params" => array(
                array(
                  "type" => "textfield",
                  "holder" => "div",
                  "class" => "",
                  "heading" => __("Title", 'domain_vc_youtube01'),
                  "param_name" => "title",
                  "value" => __("title_default", 'domain_vc_youtube01'), //default text value.
                  "description" => __("title.....", 'domain_vc_youtube01')
				  ),
					array(
					  "type" => "textfield",
					  "holder" => "div",
					  "class" => "",
					  "heading" => __("Youtube Link", 'domain_vc_youtube01'),
					  "param_name" => "strlink",
					  "value" => "https://www.youtube.com/watch?v=T70VXluhqvQ", //default text value.
					  "description" => "Type your youtube llink inside the box above..."
				  ),
			)
        ) );
    }
    
    /*
    Shortcode logic how it should be rendered
    */
    public function render_vc_youtube01( $atts, $content = null ) {
		extract( shortcode_atts( array(
			'title' => 'something',
			'strlink' => 'https://www.youtube.com/embed/T70VXluhqvQ'
		), $atts ) );
		// $content = wpb_js_remove_wpautop($content, true); // fix unclosed/unwanted paragraph tags in $content

		// $output = "<div style='color:{$color};' data-title='${title}'>".  $title.': '.$content. "</div>";
		// return $output;
	  
		$strlink = str_replace('watch?v=', 'embed/', $strlink);
		$output = "<iframe width=\"500\" height=\"400\" src=\"${strlink}\" frameborder=\"0\" allowfullscreen=\"false\"></iframe>"; 
		//https://www.youtube.com/watch?v=is4AC7BHbqw  HR
		//https://www.youtube.com/watch?v=T70VXluhqvQ Magento
		//http://www.youtube.com/embed/oHg5SJYRHA0?autoplay=0

		$output = wpb_js_remove_wpautop($output, true);
		return $output;
	  
	  
    }

    /*
    Load plugin css and javascript files which you may need on front end of your site
    */
    public function loadCssAndJs() {
      // wp_register_style( 'vc_youtube01_style', plugins_url('assets/hw_cssjs.css', __FILE__) );
      // wp_enqueue_style( 'vc_youtube01_style' );
	  
      // enqueue js on front end
      // wp_enqueue_script( 'vc_youtube01_js', plugins_url('assets/hw_cssjs.js', __FILE__), array('jquery') );
    }

    /*
    Show notice if your plugin is activated but Visual Composer is not
    */
    public function showVcVersionNotice() {
        $plugin_data = get_plugin_data(__FILE__);
        echo '
        <div class="updated">
          <p>'.sprintf(__('<strong>%s</strong> requires <strong><a href="http://bit.ly/vcomposer" target="_blank">Visual Composer</a></strong> plugin to be installed and activated on your site.', 'domain_vc_youtube01'), $plugin_data['Name']).'</p>
        </div>';
    }
}
// Finally initialize code
new VC_Youtube_Iframe();

